//
//  YXSegmentMenuContentBaseVC.m
//  YXKitOC
//
//  Created by 李鹏飞 on 2020/4/13.
//  Copyright © 2020 张鑫. All rights reserved.
//

#import "YXSegmentMenuContentBaseVC.h"
#import "TestSegmentedCustomVC.h"
#import "YXKitOC.h"
#import "JXCategoryIndicatorBackgroundView.h"

@interface YXSegmentMenuContentBaseVC () <JXCategoryViewDelegate>

@end

@implementation YXSegmentMenuContentBaseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.listContainerView];
    
    self.categoryView.listContainer = self.listContainerView;
    self.categoryView.delegate = self;
    [self.view addSubview:self.categoryView];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    self.categoryView.frame = CGRectMake(0, YX_SafeArea_Top() , self.view.bounds.size.width, 30);
    self.listContainerView.frame = CGRectMake(0, CGRectGetMaxY(self.categoryView.frame), self.view.bounds.size.width, 300);
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    //处于第一个item的时候，才允许屏幕边缘手势返回
    self.navigationController.interactivePopGestureRecognizer.enabled = (self.categoryView.selectedIndex == 0);
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    //离开页面的时候，需要恢复屏幕边缘手势，不能影响其他页面
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
}

- (JXCategoryBaseView *)preferredCategoryView {
    return [[JXCategoryTitleView alloc] init];
}

- (CGFloat)preferredCategoryViewHeight {
    return 0;
}

- (JXCategoryTitleView *)categoryView {
    if (_categoryView == nil) {
        _categoryView = [[JXCategoryTitleView alloc] init];
        _categoryView.titleLabelMaskEnabled = YES;
        _categoryView.titleColor = YX_BlackColor();
        _categoryView.titleSelectedColor = YX_RedColor();
        _categoryView.layer.cornerRadius = 15;
        _categoryView.layer.masksToBounds = YES;
        _categoryView.layer.borderColor = [UIColor redColor].CGColor;
        _categoryView.layer.borderWidth = 1/[UIScreen mainScreen].scale;
        _categoryView.titles = self.titles;
        _categoryView.cellSpacing = 0;
        CGFloat totalItemWidth = self.view.bounds.size.width - 30*2;
        _categoryView.cellWidth = totalItemWidth/self.titles.count;
        _categoryView.titleColor = [UIColor redColor];
        _categoryView.titleSelectedColor = [UIColor blueColor];
        _categoryView.titleLabelMaskEnabled = YES;
        
        JXCategoryIndicatorBackgroundView *backgroundView = [[JXCategoryIndicatorBackgroundView alloc] init];
        backgroundView.indicatorHeight = 30;
        backgroundView.indicatorWidthIncrement = 0;
        backgroundView.indicatorColor = [UIColor redColor];
        _categoryView.indicators = @[backgroundView];
    }
    return _categoryView;
}

- (JXCategoryListContainerView *)listContainerView {
    if (_listContainerView == nil) {
        _listContainerView = [[JXCategoryListContainerView alloc] initWithType:JXCategoryListContainerType_ScrollView delegate:self];
    }
    return _listContainerView;
}

#pragma mark - JXCategoryViewDelegate

- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    //侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);
    DLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
    DLog(@"%@", NSStringFromSelector(_cmd));
}

#pragma mark - JXCategoryListContainerViewDelegate

- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    TestSegmentedCustomVC *list = [[TestSegmentedCustomVC alloc] init];
    return list;
}

- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}

@end
